DSC 40B

Problems tagged with "expected time"

Problem #087

Tags: expected time

What is the expected time complexity of the following function?

import random

def foo(n):
    # randomly draw a number from 0, 1, 2, ..., n-1
    x = random.randint(0, n-1)
    if x == 1:
        for i in range(n):
            print(i)
    else:
        print("lucky!")

    for i in range(n):
        print(i)
Solution

\(\Theta(n)\).

The last loop always runs \(n\) times. With probability \(1/n\) we have x == 1, and the first loop also runs, taking \(\Theta(n)\) time; otherwise we do constant work before the last loop. So the expected time is \(\frac{1}{n} \cdot \Theta(n) + \Theta(1) + \Theta(n) = \Theta(n)\). (In fact, every case takes \(\Theta(n)\) time.)

Problem #088

Tags: expected time, time complexity

What is the expected time complexity of the following function? State your answer using asymptotic notation.

import random

def boo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
    x = random.randrange(n)

    for i in range(x): # <-- note that the range is random!
        print(i)
Solution

\(\Theta(n)\).

The loop runs \(x\) times, and \(x\) is equally likely to be any of \(0, 1, \ldots, n-1\). So the expected number of iterations is \(\frac{1}{n}\sum_{x=0}^{n-1} x = \frac{1}{n} \cdot \frac{n(n-1)}{2} = \frac{n-1}{2} = \Theta(n)\).

Problem #089

Tags: expected time

What is the expected time complexity of the function below?

import random

def foo(n):
    # draw a random number uniformly from 0, 1, 2, ..., 99
    # in constant time
    x = random.randrange(100)
    for i in range(x):
        for j in range(n):
            print("Here!")
Solution

\(\Theta(n)\).

Note that in this question, the range of \(x\) is between 0 and 99. Therefore the outer loop will run a constant number of times no matter what \(x\) is. Then we can focus on the inner loop, which calls print \(n\) times, taking \(\Theta(n)\) time. Therefore, the expected time complexity for this piece of code is \(\Theta(n)\).

Problem #093

Tags: expected time

What is the expected time complexity of the function below?

import random

def foo(n):
    # draw a random number uniformly from 0, 1, 2, ..., n-1
    # in constant time
    x = random.randrange(n)
    for i in range(x):
        for j in range(n):
            print("Here!")
Solution

\(\Theta(n^2)\).

The inner loop is not random, and the time complexity of it is always \(\Theta(n)\). Therefore the expected time complexity of this function will be (using the arithmetic sequence sum formula)

\[ \frac{1}{n} \sum_{i = 0}^{n - 1} i \times \Theta(n) = \Theta(n^2). \]

Problem #094

Tags: expected time

What is the expected time complexity of the following function?

import random

def foo(n):
    for i in range(n):
        # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
        x = random.randrange(n)
        for j in range(x):
            print(j)
Solution

\(\Theta(n^2)\).

For this function, the outer loop will execute \(n\) times no matter what, hence we only have to find what the expected time complexity for the inner loop is, then combine that together with the outer loop. For the inner loop, the expected time complexity is

\[ \frac{1}{n} \sum_{i = 1}^{n - 1} i = \frac{1}{n} \cdot \Theta(n^2) = \Theta(n). \]

Then, the expected complexity for the whole function will be

\[ \Theta(n) \times \Theta(n) = \Theta(n^2). \]

Problem #095

Tags: expected time

What is the expected time complexity of the function below?

import random

def foo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1
    # in constant time
    x = random.randrange(n)
    if x < 100:
        for j in range(n**2):
            print(j)
Solution

\(\Theta(n)\).

The for loop is only executed if \(x\) is less than 100, therefore we can write the expected time as (ignoring constant factors):

\[ f(n) = \frac{1}{n} \bigg(\sum_{i = 0}^{99} n^2 + \sum_{i = 100}^{n - 1} 1 \bigg)= \frac{1}{n} \cdot (\Theta(n^2) + \Theta(n)) = \Theta(n). \]

Problem #096

Tags: expected time

What is the expected time complexity of the following function? State your answer using asymptotic notation.

import random

def foo(n):
    x = random.randrange(n)

    if x < 8:
        for j in range(n**3):
            print(j)
    else:
        for j in range(n):
            print(j)
Solution

\(\Theta(n^2)\).

The expected time complexity for this function is

\[ \frac{1}{n} \bigg(\sum_{i = 0}^{7} n^3 + \sum_{i = 8}^{n - 1} n \bigg)= \frac{1}{n} (\Theta(n^3) + \Theta(n^2)) = \Theta(n^2). \]

Problem #097

Tags: expected time, time complexity

What is the expected time complexity of the function below? State your answer using asymptotic notation.

import random
def foo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1) time
    x = random.randrange(n)
    if x < 20:
        for i in range(n**3):
            print("Very unlucky!")
    elif x < n / 2:
        for i in range(n):
            print("Unlucky!")
    else:
        print("Lucky!")
Solution

\(\Theta(n^2)\).

The first branch happens with probability \(20/n\) and takes \(\Theta(n^3)\) time; the second happens with probability about \(1/2\) and takes \(\Theta(n)\) time; the third takes \(\Theta(1)\) time. So the expected time is about \(\frac{20}{n} \cdot n^3 + \frac12 \cdot n + \frac12 \cdot 1 = 20n^2 + \frac{n}{2} + \frac12 = \Theta(n^2)\).

Problem #098 🌶️

Tags: expected time

What is the expected time complexity of the function below?

import math
import random

def foo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
    x = random.randrange(n)
    if x < math.sqrt(n):
        for j in range(n):
            print(j)
Solution

\(\Theta(\sqrt n)\).

Since the for loop only executes when \(x\) is less than \(\sqrt{n}\), the expected time complexity is

\[ \frac{1}{n} \bigg(\sum_{i = 1}^{\lfloor \sqrt{n} \rfloor} n + \sum_{i = \lceil \sqrt{n} \rceil}^{n - 1} 1 \bigg) \approx \frac{1}{n} (n \sqrt{n} + (n - \sqrt{n})) = \Theta(\sqrt{n}). \]

Problem #099

Tags: expected time

What is the expected time complexity of the function below?

You may assume that math.sqrt takes \(\Theta(1)\) time.

import random
import math

def foo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
    x = random.randrange(n)

    if x < math.sqrt(n):
        for j in range(n):
            print(j)
    elif x < n//2:
        print('Ok!')
    else:
        for i in range(n**2):
            print(i)
Solution

\(\Theta(n^2)\).

The expected time complexity for this function is

\[ f(n) = \frac{1}{n} \bigg(\sum_{x = 0}^{\lceil \sqrt{n} \rceil - 1} n + \sum_{x = \lceil \sqrt{n} \rceil}^{\lfloor n/2 \rfloor - 1} 1 + \sum_{x = \lfloor n/2 \rfloor}^{n - 1} n^2 \bigg) = \frac{1}{n} (\Theta(n \sqrt{n}) + \Theta(n) + \Theta(n^3)) = \Theta(n^2). \]

Problem #100

Tags: expected time

What is the expected time complexity of the function below? State your answer using asymptotic notation.

You may assume that math.sqrt and math.log2 take \(\Theta(1)\) time.

import random
import math

def boo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
    x = random.randrange(n)

    if x < math.log2(n):
        for i in range(n**2):
            print("Very unlucky!")
    elif x < math.sqrt(n):
        for i in range(n):
            print("Unlucky!")
    else:
        print("Lucky!")
Solution

\(\Theta(n \log n)\)

There are three cases: Case 1) \(x < \log_2 n\); Case 2) \(\log_2 n \leq x < \sqrt n\); and Case 3) \(x \geq \sqrt{n}\).

The probability of Case 1 is \(\log n / n\). The time taken in case 1 is \(\Theta(n^2)\).

The probability of Case 2 is

\[ \frac{\sqrt n}{n} - \frac{\log_2 n}{n} = \Theta(\sqrt n / n) = \Theta(1/\sqrt n). \]

The time taken in this case is \(\Theta(n)\).

The probability of Case 3 is 1 minus the probability of the sum of the other two cases, which will simply be \(\Theta(1)\):

\[ 1 - \Theta((\log n) / n) - \Theta(1 / \sqrt{n}) = \Theta(1). \]

The time taken is \(\Theta(1)\).

Therefore, the expected time is:

\[ \begin{aligned} \frac{\log n}{n} \times \Theta(n^2) + \Theta(1/\sqrt n) \times \Theta(n) + \Theta(1) \times \Theta(1) &= \Theta(n \log n) + \Theta(\sqrt n) + \Theta(1) \\ &= \Theta(n \log n) \end{aligned} \]

Problem #101

Tags: expected time

What is the expected time complexity of the function below? State your answer using asymptotic notation.

You may assume that math.sqrt and math.log take \(\Theta(1)\) time. math.log computes the natural log.

import random
import math

def foo(n):
    # draw a number uniformly at random from 0, 1, 2, ..., n-1 in Theta(1)
    x = random.randrange(n)

    if x < math.log(n):
        for j in range(n**2):
            print(j)
    elif x < math.sqrt(n):
        print('Ok!')
    else:
        for i in range(n):
            print(i)
Solution

\(\Theta(n \log n)\)

There are three cases: Case 1) \(x < \log n\); Case 2) \(\log n \leq x < \sqrt n\); and Case 3) \(x \geq \sqrt{n}\).

The probability of Case 1 is \(\log n / n\). The time taken in case 1 is \(\Theta(n^2)\).

The probability of Case 2 is

\[ \frac{\sqrt n}{n} - \frac{\log n}{n} = \Theta(\sqrt n / n) = \Theta(1/\sqrt n). \]

The time taken in this case is \(\Theta(1)\).

The probability of Case 3 is 1 minus the probability of the sum of the other two cases, which will simply be \(\Theta(1)\):

\[ 1 - \Theta((\log n) / n) - \Theta(1 / \sqrt{n}) = \Theta(1) \]

Therefore, the expected time is:

\[ \begin{aligned} \frac{\log n}{n} \times \Theta(n^2) + \Theta(1/\sqrt n) \times \Theta(1) + \Theta(1) \times \Theta(n) &= \Theta(n \log n) + \Theta(1 / \sqrt n) + \Theta(n) \\ &= \Theta(n \log n) \end{aligned} \]